package com.gensdai.leliang.dialog;

import android.app.Activity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.EditText;
import android.widget.Toast;

import com.flyco.roundview.RoundTextView;
import com.gensdai.leliang.R;
import com.gensdai.leliang.popupwindow.BasePopupWindow;
import com.gensdai.leliang.utils.BaseUtils;

/**
 * Created by Administrator on 2017/6/6.
 */

public class dialog_setup_password extends BasePopupWindow {

    private View popupView;
    private RoundTextView done;
    private RoundTextView cancelBtn;
    private EditText passwordtext;
    private EditText confimpasswordtext;


    OnMenuClick click;


    public void setOnMenuClick(OnMenuClick click){
        this.click = click;
    }

    public dialog_setup_password(Activity context) {
        super(context);

        bindEvent();
    }

    @Override
    public View onCreatePopupView() {
        popupView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_setup_password, null);
        return popupView;
    }

    //初始化
    private void bindEvent() {
        if (popupView != null) {
            done = (RoundTextView) popupView.findViewById(R.id.done);
            passwordtext = (EditText) popupView.findViewById(R.id.pass);
            confimpasswordtext = (EditText) popupView.findViewById(R.id.confimPass);
            cancelBtn = (RoundTextView) popupView.findViewById(R.id.cancelBtn);

            done.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    invalidate();
                }
            });
            cancelBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(click != null){
                        click.onCancelClick();
                        dismiss();
                    }
                }
            });

        }
    }

    private void invalidate(){

        String password = passwordtext.getText().toString().trim();
        String confim = confimpasswordtext.getText().toString().trim();

        if(TextUtils.isEmpty(password)){
            Toast.makeText(getContext(), "请输入支付密码", Toast.LENGTH_SHORT).show();
            return;
        }else if(TextUtils.isEmpty(confim)){
            Toast.makeText(getContext(), "请输入确认支付密码", Toast.LENGTH_SHORT).show();
            return;
        }else if(password.length() < 6){
            Toast.makeText(getContext(), "支付密码长度6位请输入6位数的密码", Toast.LENGTH_SHORT).show();
            return;
        }else if(TextUtils.equals(password,confim)){
            if(click != null){
                click.onDoneClick(BaseUtils.EncryptedPassword(password),BaseUtils.EncryptedPassword(confim));
            }
        }else{
            Toast.makeText(getContext(), "支付密码与确认密码不匹配", Toast.LENGTH_SHORT).show();
            return;
        }


    }

    @Override
    public View initAnimaView() {
        return popupView.findViewById(R.id.setup_contentdialoglayout);
    }

    @Override
    protected Animation initShowAnimation() {
        return getDefaultScaleAnimation();
    }

    @Override
    public View getClickToDismissView() {
        return getPopupWindowView();
    }


    /*interface 回调*/
    public interface OnMenuClick{
        void onDoneClick(String pass1,String pass2);
        void onCancelClick();
    }
}
